package chapter.second.item6;

import java.util.Arrays;
import java.util.Map;
import java.util.WeakHashMap;

public class Stack {
	private Object[] elements;
	private int size = 0;
	private static final int DEFAULT_INITIAL_CAPACITY = 16;
	
	public Stack(){
		elements = new Object[DEFAULT_INITIAL_CAPACITY];
	}
	
	public void push(Object e){
		ensureCapacity();
		elements[size++] = e;
	}

	private void ensureCapacity() {
		if(elements.length==size){
			elements = Arrays.copyOf(elements, 2*size+1);
		}
	}
	
	public Object pop() throws IllegalAccessException{
		if(elements.length==0){
			throw new IllegalAccessException();
		}
		//return elements[--size]; 未消除引用，会造成内存泄露
		Object e = elements[--size];
		elements[size] = null;
		return e;
	}
	
}
